package cn.itcast.bos.service.fixedarea.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.CourierRepository;
import cn.itcast.bos.dao.FixedAreaRepository;
import cn.itcast.bos.dao.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.fixedarea.FixedAreaService;

@Service
@Transactional
public class FixedAreaServiceImp implements FixedAreaService {
	@Autowired
	private FixedAreaRepository fixedAreaRepository;
	
	@Autowired
	private CourierRepository courierRepository;
	
	@Autowired
	private TakeTimeRepository takeTimeRepository;

	private FixedArea FixedArea ;
	public void save(FixedArea model) {
		fixedAreaRepository.save(model);

	}

	@Override
	public Page findPageData(Specification<FixedArea> specification,
			Pageable pageable) {

		return fixedAreaRepository.findAll(specification, pageable);
	}

	public void delete(String ids) {

		String[] arr = ids.split("-");
		for (String id : arr) {
			FixedArea f = fixedAreaRepository.findOne(id);
			fixedAreaRepository.delete(f);
		}

	}

	@Override
	public void linkedCourier(FixedArea model, Integer courierId,
			Integer takeTimeId) {
		//关联就是设置属性  维护关联关系的一方来set属性
		FixedArea fixedArea = fixedAreaRepository.findOne(model.getId());
		Courier courier = courierRepository.findOne(courierId);
		TakeTime taketime = takeTimeRepository.findOne(takeTimeId);
		fixedArea.getCouriers().add(courier);
		courier.setTakeTime(taketime);
		
		
	}

	public void deleteCourierToFixedArea(String fixedid, String nocourierId) {
		FixedArea fixedArea = fixedAreaRepository.findOne(fixedid);
		Courier courier = courierRepository.findOne(Integer.parseInt(nocourierId));
		fixedArea.getCouriers().remove(courier);
		
	}

}



















